
package sn.gandal.mobile.kalpet.android.guichet;

import sn.gandal.mobile.kalpet.android.guichet.response.AccountResponse;
import sn.gandal.mobile.kalpet.android.guichet.response.ExceptionCode;
import sn.gandal.mobile.kalpet.persistence.Account;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/** Example resource class hosted at the URI path "/myresource"
 */
@Path("/guichetResources")
@Stateless
public class GuichetMobileService {
    
    /** Method processing HTTP GET requests, producing "text/plain" MIME media
     * type.
     * @return String that will be send back as a response of type "text/plain".
     */
   
    //@PersistenceUnit(unitName = "guichet")
	private EntityManager em;

   

  /**
    @GET
	@Produces({"application/xml"})
	public AuthResponse login(@QueryParam(value = "username") @DefaultValue(value = "sowdiomyero") final String username,
		                   @QueryParam(value = "password") @DefaultValue(value = "adamaseme") final String password) {
		// TODO ...do some stuff...
		AuthResponse response = new AuthResponse();
		response.code=200;
		response.message="Utilisateur"+username+"- et qui a le mot de passe "+ password+" ==> Login OK";
		return response;
	}  **/

    @GET
	@Produces(MediaType.APPLICATION_JSON)
    public AccountResponse getAuthUser(@QueryParam(value = "numeroClient") @DefaultValue(value = "30866200") final String numeroClient,
		                   @QueryParam(value = "codeSecret") @DefaultValue(value = "260482") final String codeSecret) {

        AccountResponse response = new AccountResponse();

       Integer numCli=Integer.parseInt(numeroClient);
       Integer code=Integer.parseInt(codeSecret);
       Account account=null;

      Query query=null;
       try{
           em= PersistenceService.getInstance().getEntityManager();
           query =em.createNamedQuery("Account.findByNumeroClientAndCodeSecret");
           query.setParameter("numeroClient",numCli);
           query.setParameter("codeSecret",code);
           account= (Account)query.getSingleResult();
           if(account != null){
               response.cloneAccount(account);
               response.codeRetour= ExceptionCode.CODES.COMPTE_VALIDE.getValue();
               return response;
           } else {
              response.codeRetour= ExceptionCode.CODES.COMPTE_INACTIF.getValue();
              return response;
           }

         } catch (NoResultException nrez) {
           response.codeRetour= ExceptionCode.CODES.MDP_INVALIDE.getValue();
           nrez.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
       } catch (RuntimeException e) {
           e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
           response.codeRetour= ExceptionCode.CODES.MDP_INVALIDE.getValue();
       }
      return response;
  }
    	public EntityManager getEntityManager() {
		return em;
	}

	
}
